package com.washmadam.service.domain.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.washmadam.service.common.enums.PeriodTypeEnum;
import com.washmadam.service.common.enums.ReserveStatusEnum;
import com.washmadam.service.common.enums.ServiceStatusEnum;
import com.washmadam.service.common.enums.StatusEnum;
import com.washmadam.service.common.utils.DateUtils;
import com.washmadam.service.db.dto.CustomerOrder;
import com.washmadam.service.db.dto.CustomerOrderService;
import com.washmadam.service.db.request.operate.CustomerOrderOperateParam;
import com.washmadam.service.service.customer.CustomerOperateService;
import com.washmadam.service.service.order.CustomerOrderOperateService;
import com.washmadam.service.service.order.CustomerOrderQueryService;
import com.washmadam.service.service.service.CustomerOrderServiceOperateService;
import com.washmadam.service.service.service.CustomerOrderServiceQueryService;

@Component
public class CustomerOrderServiceDomain {
	@Autowired
	CustomerOperateService customerOperateService;
	@Autowired
	CustomerOrderServiceQueryService customerOrderServiceQueryService;
	@Autowired
	CustomerOrderServiceOperateService customerOrderServiceOperateService;
	@Autowired
	CustomerOrderOperateService customerOrderOperateService;
	@Autowired 
	CustomerOrderQueryService customerOrderQueryService;

	public void addServiceReserve(CustomerOrderService requestParam) {
		CustomerOrder customerOrder = customerOrderQueryService.getCustomerOrderById(requestParam.getCustomerOrderId());
		if(customerOrder==null) {
			throw new RuntimeException("订单查询失败");
		}
		if(customerOrder.getReserveStatus() == ReserveStatusEnum.RESERVE.getCode()) {
			throw new RuntimeException("订单不可重复预约");
		}
		if(customerOrder.getCurrentNum()<=0 
				|| customerOrder.getReserveStatus() == ReserveStatusEnum.MEMBER_END.getCode()) {
			throw new RuntimeException("订单服务次数已用完");
		}
		if(StringUtils.isBlank(requestParam.getCustomerName())) {
			requestParam.setCustomerName(customerOrder.getCustomerName());
		}
		if(StringUtils.isBlank(requestParam.getCustomerAddress())) {
			requestParam.setCustomerAddress(customerOrder.getCustomerAddress());
		}
		if(StringUtils.isBlank(requestParam.getContactMobile())) {
			requestParam.setContactMobile(customerOrder.getContactMobile());
		}
		if(StringUtils.isBlank(requestParam.getOtherContactMobile())) {
			requestParam.setOtherContactMobile(customerOrder.getOtherContactMobile());
		}
		//保存预约服务信息
		CustomerOrderService customerOrderService = customerOrderServiceQueryService.getCurrentCustomerOrderServiceByOrderId(customerOrder.getId(),(customerOrder.getTotalNum()-customerOrder.getCurrentNum()+1));
		requestParam.setId(customerOrderService.getId());
		requestParam.setCustomerId(customerOrder.getCustomerId());
		requestParam.setServiceStatus(ServiceStatusEnum.UN_COMPLETED.getCode());
		customerOrderServiceOperateService.saveOrUpdateCustomerOrderService(requestParam);
		//同步预约信息到订单信息
		CustomerOrderOperateParam customerOrderOperateParam = new CustomerOrderOperateParam(); 
		customerOrderOperateParam.setId(customerOrder.getId());
		customerOrderOperateParam.setNextServiceDate(requestParam.getAppointmentDate());
		//预定日期不等于原定日期，则下期
		if(!DateUtils.formatDate(customerOrder.getNextServiceDate(),DateUtils.DATE_FORMAT)
				.equals(DateUtils.formatDate(customerOrderOperateParam.getNextServiceDate(),DateUtils.DATE_FORMAT))) {
			Date endServiceDate;
			if(customerOrder.getPeriodType()==null || customerOrder.getPeriodType()==PeriodTypeEnum.DAY.getType()) {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.DAY.getType());
				endServiceDate = DateUtils.addDays(DateUtils.getBeginDate(requestParam.getAppointmentDate()), (customerOrder.getServicePeriod()*(customerOrder.getCurrentNum()-1)));
			}else {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.MONTH.getType());
				endServiceDate = DateUtils.addMonths(DateUtils.getBeginDate(requestParam.getAppointmentDate()), (customerOrder.getServicePeriod()*(customerOrder.getCurrentNum()-1)));
			}
			customerOrderOperateParam.setEndServiceDate(endServiceDate);
		}
		customerOrderOperateParam.setReserveStatus(ReserveStatusEnum.RESERVE.getCode());
		customerOrderOperateParam.setUpdateTime(new Date());
		customerOrderOperateParam.setUpdateBy(requestParam.getUpdateBy());
		customerOrderOperateService.saveOrUpdateOrder(customerOrderOperateParam);
	}

	public void serviceComplete(CustomerOrderService requestParam) {
		CustomerOrder customerOrder = customerOrderQueryService.getCustomerOrderById(requestParam.getCustomerOrderId());
		if(customerOrder==null) {
			throw new RuntimeException("订单查询失败");
		}
		if(customerOrder.getCurrentNum()<=0 
				|| customerOrder.getReserveStatus() == ReserveStatusEnum.MEMBER_END.getCode()) {
			throw new RuntimeException("订单服务次数已用完");
		}
		if(customerOrder.getReserveStatus() != ReserveStatusEnum.RESERVE.getCode()) {
			throw new RuntimeException("订单未预约,不可点完成");
		}
		//保存预约服务信息
		CustomerOrderService customerOrderService = customerOrderServiceQueryService.getCurrentCustomerOrderServiceByOrderId(customerOrder.getId(),(customerOrder.getTotalNum()-customerOrder.getCurrentNum()+1));
		requestParam.setId(customerOrderService.getId());
		requestParam.setCustomerId(customerOrder.getCustomerId());
		requestParam.setServiceStatus(ServiceStatusEnum.COMPLETED.getCode());
		customerOrderServiceOperateService.saveOrUpdateCustomerOrderService(requestParam);
		//初始化下一期服务数据
		if(customerOrder.getCurrentNum()>1) {
			CustomerOrderService nextCustomerOrderService = new CustomerOrderService();
			nextCustomerOrderService.setCustomerId(customerOrder.getCustomerId());
			nextCustomerOrderService.setCustomerName(customerOrder.getCustomerName());
			nextCustomerOrderService.setCustomerAddress(customerOrder.getCustomerAddress());
			nextCustomerOrderService.setContactMobile(customerOrder.getContactMobile());
			nextCustomerOrderService.setOtherContactMobile(customerOrder.getOtherContactMobile());
			nextCustomerOrderService.setServiceNum(customerOrderService.getServiceNum()+1);
			nextCustomerOrderService.setCustomerOrderId(requestParam.getCustomerOrderId());
			nextCustomerOrderService.setStatus(StatusEnum.ENABLE.getCode());
			nextCustomerOrderService.setCreateTime(new Date());
			nextCustomerOrderService.setCreateBy(requestParam.getUpdateBy());
			nextCustomerOrderService.setUpdateBy(requestParam.getUpdateBy());
			nextCustomerOrderService.setUpdateTime(new Date());
			customerOrderServiceOperateService.saveOrUpdateCustomerOrderService(nextCustomerOrderService);
		}
		//更新订单信息
		CustomerOrderOperateParam customerOrderOperateParam = new CustomerOrderOperateParam(); 
		customerOrderOperateParam.setId(customerOrder.getId());
		customerOrderOperateParam.setCurrentNum(customerOrder.getCurrentNum()-1);
		customerOrderOperateParam.setLastServiceDate(customerOrderService.getAppointmentDate());
		customerOrderOperateParam.setLastServicePerson(requestParam.getHandlePerson());
		customerOrderOperateParam.setReserveStatus(ReserveStatusEnum.UN_RESERVE.getCode());
		if(customerOrderOperateParam.getCurrentNum()==0) {
			customerOrderOperateParam.setReserveStatus(ReserveStatusEnum.MEMBER_END.getCode());
		}
		Date nextServiceDate = null;
		Date endServiceDate = null;
		if(customerOrderOperateParam.getCurrentNum() >0) {
			if(customerOrder.getPeriodType()==null ||customerOrder.getPeriodType()==PeriodTypeEnum.DAY.getType()) {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.DAY.getType());
				nextServiceDate = DateUtils.addDays(DateUtils.getBeginDate(customerOrderService.getAppointmentDate()), customerOrder.getServicePeriod());
				endServiceDate = DateUtils.addDays(nextServiceDate, (customerOrder.getServicePeriod()*(customerOrderOperateParam.getCurrentNum()-1)));
			}else {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.MONTH.getType());
				nextServiceDate = DateUtils.addMonths(DateUtils.getBeginDate(customerOrderService.getAppointmentDate()), customerOrder.getServicePeriod());
				endServiceDate = DateUtils.addMonths(nextServiceDate, (customerOrder.getServicePeriod()*(customerOrderOperateParam.getCurrentNum()-1)));
			}
		}else {
			nextServiceDate = customerOrderService.getAppointmentDate();
			endServiceDate = customerOrderService.getAppointmentDate();
		}
		customerOrderOperateParam.setNextServiceDate(nextServiceDate);
		customerOrderOperateParam.setEndServiceDate(endServiceDate);
		customerOrderOperateParam.setUpdateBy(requestParam.getUpdateBy());
		customerOrderOperateParam.setUpdateTime(new Date());
		customerOrderOperateService.saveOrUpdateOrder(customerOrderOperateParam);
		
		customerOperateService.addServiceNum(customerOrder.getCustomerId(),1);
	}

	public void serviceCancel(CustomerOrderService requestParam) {
		CustomerOrder customerOrder = customerOrderQueryService.getCustomerOrderById(requestParam.getCustomerOrderId());
		if(customerOrder==null) {
			throw new RuntimeException("订单查询失败");
		}
		if(customerOrder.getCurrentNum()<=0 
				|| customerOrder.getReserveStatus() == ReserveStatusEnum.MEMBER_END.getCode()) {
			throw new RuntimeException("订单服务次数已用完");
		}
		if(customerOrder.getReserveStatus() != ReserveStatusEnum.RESERVE.getCode()) {
			throw new RuntimeException("订单未预约,不可取消");
		}
		//保存预约服务信息
		CustomerOrderService customerOrderService = customerOrderServiceQueryService.getCurrentCustomerOrderServiceByOrderId(customerOrder.getId(),(customerOrder.getTotalNum()-customerOrder.getCurrentNum()+1));
		requestParam.setId(customerOrderService.getId());
		requestParam.setCustomerId(customerOrder.getCustomerId());
		requestParam.setServiceStatus(ServiceStatusEnum.NORMAL.getCode());
		customerOrderServiceOperateService.saveOrUpdateCustomerOrderService(requestParam);
		//更新订单信息
		CustomerOrderOperateParam customerOrderOperateParam = new CustomerOrderOperateParam(); 
		customerOrderOperateParam.setId(customerOrder.getId());
		customerOrderOperateParam.setNextServiceDate(requestParam.getAppointmentDate());
		//预定日期不等于原定日期，则下期
		if(!DateUtils.formatDate(customerOrder.getNextServiceDate(),DateUtils.DATE_FORMAT)
				.equals(DateUtils.formatDate(customerOrderOperateParam.getNextServiceDate(),DateUtils.DATE_FORMAT))) {
			Date endServiceDate;
			if(customerOrder.getPeriodType()==null || customerOrder.getPeriodType()==PeriodTypeEnum.DAY.getType()) {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.DAY.getType());
				endServiceDate = DateUtils.addDays(DateUtils.getBeginDate(requestParam.getAppointmentDate()), (customerOrder.getServicePeriod()*(customerOrder.getCurrentNum()-1)));
			}else {
				customerOrderOperateParam.setPeriodType(PeriodTypeEnum.MONTH.getType());
				endServiceDate = DateUtils.addMonths(DateUtils.getBeginDate(requestParam.getAppointmentDate()), (customerOrder.getServicePeriod()*(customerOrder.getCurrentNum()-1)));
			}
			customerOrderOperateParam.setEndServiceDate(endServiceDate);
		}
		customerOrderOperateParam.setReserveStatus(ReserveStatusEnum.UN_RESERVE.getCode());
		customerOrderOperateParam.setUpdateBy(requestParam.getUpdateBy());
		customerOrderOperateParam.setUpdateTime(new Date());
		customerOrderOperateService.saveOrUpdateOrder(customerOrderOperateParam);
		
	}


}
